package com.molichuxing.services.infrastructure.service.impl;

import com.molichuxing.services.infrastructure.bean.UserDetailBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.UserDetailMapper;
import com.molichuxing.services.infrastructure.dto.request.create.UserDetailCreateDto;
import com.molichuxing.services.infrastructure.dto.request.modify.UserDetailModifyDto;
import com.molichuxing.services.infrastructure.dto.response.UserDetailDto;
import com.molichuxing.services.infrastructure.service.UserDetailService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liyg
 * 用户详情服务实现类
 * 2019-08-13
 */
@Service("userDetailService")
public class UserDetailServiceImpl implements UserDetailService {

    @Autowired
    private UserDetailMapper userDetailMapper;


    /**
     * 获取用户详情信息
     *
     * @param userId 用户id
     * @return
     */
    @Override
    public UserDetailDto getUserDetail(Integer userId) {

        UserDetailBean userDetail = userDetailMapper.getById(userId);

        UserDetailDto beanDto = Convert.toUserDetailDto(userDetail);

        return beanDto;
    }

    /**
     * 获取用户详情信息
     *
     * @param userIds 用户ids
     * @return
     */
    @Override
    public Map<Integer, UserDetailDto> getUserDetails(List<Integer> userIds) {

        if (null == userIds || userIds.size() <= 0) {
            return null;
        }

        List<UserDetailBean> userDetailBeanList = userDetailMapper.getByIds(userIds);
        List<UserDetailDto> userDetailDtoList = Convert.toUserDetailDto(userDetailBeanList);
        if (null == userDetailDtoList || userDetailDtoList.size() <= 0) {
            return null;
        }

        Integer initialCapacity = userDetailBeanList.size();
        Map<Integer, UserDetailDto> map = new HashMap<Integer, UserDetailDto>(initialCapacity);
        for (UserDetailDto dto : userDetailDtoList) {
            map.put(dto.getId(), dto);
        }

        return map;
    }

    /**
     * 保存用户详情信息
     *
     * @param createDto
     * @return
     */
    @Override
    public Boolean create(UserDetailCreateDto createDto) {
        UserDetailBean bean = new UserDetailBean();
        BeanUtils.copyProperties(createDto, bean);
        int num = userDetailMapper.create(bean);
        return num == 1;
    }

    /**
     * 修改用户详情信息
     *
     * @param modifyDto
     * @return
     */
    @Override
    public Boolean modifyRegionId(UserDetailModifyDto modifyDto) {
        UserDetailBean bean = new UserDetailBean();
        BeanUtils.copyProperties(modifyDto, bean);
        int num = userDetailMapper.modifyRegionId(bean);
        return num == 1;
    }

    /**
     * 修改用户详情信息
     *
     * @param modifyDto
     * @return
     */
    @Override
    public Boolean modifyBirthday(UserDetailModifyDto modifyDto) {
        UserDetailBean bean = new UserDetailBean();
        BeanUtils.copyProperties(modifyDto, bean);
        int num = userDetailMapper.modifyBirthday(bean);
        return num == 1;
    }

    /**
     * 修改用户地址
     *
     * @param regionId
     * @param userId
     * @return
     */
    @Override
    public Boolean modifyRegionIdById(Integer regionId, Integer userId) {
        int num = userDetailMapper.modifyRegionIdById(regionId, userId);
        return num == 1;
    }

    /**
     * 查询性别分布统计
     *
     * @param isPay 是否支付过
     * @return
     */
    @Override
    public List<Map<String, Object>> getSexDistribution(Boolean isPay) {
        return userDetailMapper.getSexDistribution(isPay);
    }

    /**
     * 查询用户年龄
     *
     * @param isPay 是否支付过
     * @return
     */
    @Override
    public List<Map<String, Object>> getAllAge(Boolean isPay) {
        return userDetailMapper.getAllAge(isPay);
    }

    /**
     * 查询所有付过款的用户
     *
     * @return
     */
    @Override
    public Integer getPayUserCount() {
        return userDetailMapper.getPayUserCount();
    }

    /**
     * 将用户置为已支付
     *
     * @param userId
     * @return
     */
    @Override
    public int modifyUserPayByUserId(Integer userId) {
        return userDetailMapper.modifyUserPayByUserId(userId);
    }
}
